package svgedit.gui.command;

import java.util.ArrayList;

import svgedit.gui.View;
import svgedit.svg.SVGElement;
import svgedit.svg.SVGVisitor;

/**
 *  Define move exectution as a command object with functions
 *     execute (redo) and undo
 * */
public class MoveCommand implements Command{
	
	private SVGVisitor visitor;
	private SVGElement[] elements;
	private ArrayList<SVGElement> backupElems;
	private SVGVisitor visitorback;
	private View view;
	
	/**
	 *  Constructor for move command class
	 *  
	 *  @param view to get view object
	 *  @param visitor to get the move visitor
	 *  @param visitor to get the opposite move visitor
	 *  @param elements to get the list of the elements which are moved
	 * */
	public MoveCommand(View view,SVGVisitor visitor,SVGVisitor visitorback,SVGElement[] elements){
		this.visitor = visitor;
		this.elements = elements;
		this.visitorback = visitorback;
		backupElems = new ArrayList<SVGElement>();
		this.view = view;
		
	}
	
	/**
	 *   Execute when move the elements
	 *      or redo the move the elements
	 * */
	public void execute(){
		if(backupElems.size() == 0){
			for (SVGElement element : elements){
				backupElems.add(element);
			}
		}else{
			for (SVGElement element : elements) {
	            element.acceptVisitor(visitor);
	        }
			view.repaint();
		}
		 
	}
	
	/**
	 *  Undo the move method to recover the elements original position
	 * */
	public void undo(){
		for (SVGElement element : backupElems) {
            element.acceptVisitor(visitorback);
        }
		view.repaint();
	}

}
